<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\TaskModel;
use common\service\DemandPoolReportService;
use common\service\StatusService;
use common\service\StepSkuService;
use common\service\TaskUpstreamService;
use Webman\Stomp\RetryAbleConsumer;

/**
 * 同步需求池
 */
class DemandPoolReportConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名
    /**
     * Summary of queue
     * @var string
     */
    public $queue = 'sync_demand_pool_report';

    /**
     * Summary of connection
     * @var string
     */
    public $connection = 'default';


    /**
     * 同步需求池
     * @param $data
     * @return void
     */
    public function doConsume($data)
    {
        //同步需求池--废除、启用实体
        if (!empty($data['project_id']) && !empty($data['initial_episode_code'])) {
            DemandPoolReportService::getInstance()->syncDemandPoolByEpisode($data['project_id'], $data['initial_episode_code']);
            return;
        }
        //同步需求池--实体ID
        if (isset($data['entity_ids'])) {
            DemandPoolReportService::getInstance()->syncDemandPoolByEntityIds($data['entity_ids']);
            return;
        }
        //同步需求池--排期ID
        if (isset($data['plan_id'])) {
            DemandPoolReportService::getInstance()->syncDemandPoolByPlanId($data['plan_id']);
        }
    }

    /**
     * Summary of getTtr
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * Summary of canRetry
     * @param mixed $attempt
     * @param mixed $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
